package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.client.dao.log.RefundOrderDao;
import com.tbit.uqbike.client.pojo.RefundOrder;
import com.tbit.uqbike.client.service.RefundOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 退款订单（退款中间表）Service实现类
 *
 * @author <a href="mailto:chen.wutian@tbit.com.cn">BUGTian</a>
 * @since 2025/2/6
 */
@Service
@Slf4j
public class RefundOrderServiceImpl implements RefundOrderService {

    @Resource
    private RefundOrderDao refundOrderDao;


    @Override
    public RefundOrder fetchRefundOrderWithRetry(String refundNO, int maxRetries, long delayMillis) {
        for (int attempt = 0; attempt <= maxRetries; attempt++) {
            sleep(delayMillis);
            RefundOrder refundOrder = refundOrderDao.getByRefundNO(refundNO);
            // 记录存在且记录完整
            if (refundOrder != null) {
                return refundOrder;
            }
            log.info("退款订单号：{}，第{}次重试查询...", refundNO, attempt + 1);
        }
        log.error("退款订单号：{}，经过{}次重试仍未获取完整数据", refundNO, maxRetries);
        return null;
    }

    private static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            log.error("查询被中断");
        }
    }

}
